package com.qianfeng.zhangwei.day28.widget;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

import com.qianfeng.zhangwei.day28.R;

/**
 * @说 明: 自定圆形进度条
 * @项目名称：Sz1504
 * @包 名： com.qianfeng.zhangwei.day28
 * @类 名： ProgressView
 * @创 建人： zhangwei
 * @创建时间：2015-07-22 23:00
 * @版 本：v1.0
 * @修 改人：
 * @修改时间：
 * @修改备注：
 */
public class ProgressView extends View {
    private Paint paint = new Paint();
    private int max = 100;
    private int progress = 50;
    private int roundColor;

    private int roundProgressColor;
    private int roundWidth;
    private int textColor;
    private int textSize;
    private int strokeStyle;
    private static final int STROKE_STYLE = 0;


    public static final int FILL_STYLE = 1;


    public ProgressView(Context context) {
        super(context);
    }

    public ProgressView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public ProgressView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundProgressBar);
        roundColor = typedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.GREEN);
        roundProgressColor = typedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, Color.RED);
        roundWidth = (int) typedArray.getDimension(R.styleable.RoundProgressBar_roundWidth, 10);
        textColor = typedArray.getColor(R.styleable.RoundProgressBar_textColor, Color.BLACK);
        textSize = (int) typedArray.getDimension(R.styleable.RoundProgressBar_textSize, 15);
        strokeStyle = typedArray.getInt(R.styleable.RoundProgressBar_style, 0);
        typedArray.recycle();
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        /**
         * 第一步绘制最外层的圆
         */

        switch (strokeStyle) {
            case 0:
                paint.setStyle(Paint.Style.STROKE);
                break;
            case 1:
                paint.setStyle(Paint.Style.FILL);
                break;
        }
        paint.setStrokeWidth(roundWidth);
        paint.setColor(roundColor);
        paint.setAntiAlias(true);//处理图片 消耗内存
        int centerWidth = getWidth() / 2;
        int radius = centerWidth - roundWidth;
        canvas.drawCircle(centerWidth, centerWidth, radius, paint);
        /**
         * 绘制进度 文字  70%
         */
        paint.setColor(textColor);
        paint.setTextSize(textSize);
        String progressStr = (int) ((float) progress / (float) max * 100) + "%";
        int textmeasureWidth = (int) (centerWidth - paint.measureText(progressStr) / 2);
        canvas.drawText(progressStr, textmeasureWidth, centerWidth, paint);
        paint.setColor(roundColor);
        switch (strokeStyle) {
            case 0:
                paint.setStyle(Paint.Style.STROKE);
                break;
            case 1:
                paint.setStyle(Paint.Style.FILL);
                break;
        }
        paint.setStrokeWidth(roundWidth);
        RectF rectF = new RectF(centerWidth - radius, centerWidth - radius, centerWidth + radius, centerWidth + radius);
        canvas.drawArc(rectF, 0f, ((float) progress / (float) max * 360), false, paint);

    }

    public Paint getPaint() {
        return paint;
    }

    public void setPaint(Paint paint) {
        this.paint = paint;
        invalidate();//刷新当前的界面
        postInvalidate();//异步刷新



    }

    public int getMax() {
        return max;
    }

    public void setMax(int max) {
        this.max = max;
    }

    public int getProgress() {
        return progress;
    }

    public void setProgress(int progress) {
        this.progress = progress;
    }

    public int getRoundColor() {
        return roundColor;
    }

    public void setRoundColor(int roundColor) {
        this.roundColor = roundColor;
    }

    public int getRoundProgressColor() {
        return roundProgressColor;
    }

    public void setRoundProgressColor(int roundProgressColor) {
        this.roundProgressColor = roundProgressColor;
    }

    public int getRoundWidth() {
        return roundWidth;
    }

    public void setRoundWidth(int roundWidth) {
        this.roundWidth = roundWidth;
    }

    public int getTextColor() {
        return textColor;
    }

    public void setTextColor(int textColor) {
        this.textColor = textColor;
    }

    public int getTextSize() {
        return textSize;
    }

    public void setTextSize(int textSize) {
        this.textSize = textSize;
    }

    public int getStrokeStyle() {
        return strokeStyle;
    }

    public void setStrokeStyle(int strokeStyle) {
        this.strokeStyle = strokeStyle;
    }
}
